package com.whz.governance.http.ribbon.mvc;

import com.whz.governance.http.ribbon.Constants;
import lombok.experimental.UtilityClass;
import org.springframework.util.StringUtils;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @author whz
 */
@UtilityClass
public class GreyFlagUtils {

    public String getFlag(final HttpServletRequest request) {
        String tag = request.getHeader(Constants.GREY_HEAD_KEY);
        if (StringUtils.hasText(tag)) {
            return tag;
        }
        Cookie cookie = WebUtils.getCookie(request, Constants.GREY_HEAD_KEY);
        if (cookie != null) {
            tag = cookie.getValue();
        }
        return tag;
    }
}